home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-16 | 6.0 KB | 212 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CEdge.h ©1995-97 Timo Eloranta All rights reserved.
- // ===========================================================================
- // A simple edge class. The edge is built of two pointers pointing to the
- // nodes between which this edge exists...
-
- #pragma once
-
- #include "CNode.h"
-
- class CEdge
- {
- protected:
- CNodePtr mNode1; // Pointer to the 1st node
- CNodePtr mNode2; // Pointer to the 2nd node
- short mLength; // Square of the length...
-
- public:
- CEdge();
-
- void Set( const CNodePtr inNode1,
- const CNodePtr inNode2);
-
- void GetEdgeNodes( CNodePtr & outPtr1,
- CNodePtr & outPtr2 ) const;
-
- void GetNodeNumbers( short & outNbr1,
- short & outNbr2) const;
-
- Boolean operator==( const CEdge & inEdge) const;
- Boolean Intersects( const CEdge & inEdge) const;
- Boolean HasMutualNode( const CEdge & inEdge) const;
-
- Boolean OtherNode( const CNodePtr & inNode,
- CNodePtr & outNode) const;
-
- Int16 SetLength( );
- Int16 GetLength( ) const { return mLength; };
-
- void Draw( Int16 inOneOneXY, Int16 inSquareSize) const;
-
- Boolean HasMoved() const;
- };
-
- null_template
- struct iterator_trait <const CEdge*> {
- typedef ptrdiff_t distance_type;
- typedef const CEdge value_type;
- typedef random_access_iterator_tag iterator_category;
- };
-
- // ===========================================================================
- // • Inline Functions
- // ===========================================================================
-
- // ---------------------------------------------------------------------------
- // • Set
- //
- // Called by: CEdge::CEdge
- // CGraphDrawing::ValidNewEdge
- // CGraphDrawing::SetNewEdge
- // CGraphDrawing::CopyEdges
- // ---------------------------------------------------------------------------
- // Define this edge by setting the nodes (inNode1 & 2)
- // between which the edge exists.
-
- inline void
- CEdge::Set( const CNodePtr inNode1, const CNodePtr inNode2)
- {
- mNode1 = inNode1;
- mNode2 = inNode2;
- }
-
- // ---------------------------------------------------------------------------
- // • CEdge
- //
- // Called by: CGraphDrawing::Initialize
- // CGraphDrawing::ValidNewEdge
- // CGraphDrawing::CopyEdges
- // ---------------------------------------------------------------------------
- // Constructor. Set nodes to nil and length to 0...
-
- inline
- CEdge::CEdge()
- {
- Set( nil, nil);
- mLength = 0;
- }
-
- // ---------------------------------------------------------------------------
- // • operator==
- //
- // Called by: CGraphDrawing::ValidNewEdge
- // ---------------------------------------------------------------------------
- // Return true if inEdge is the same edge as this one. Otherwise return false.
-
- inline Boolean
- CEdge::operator==( const CEdge & inEdge ) const
- {
- // Simply comparing node pointers is dirty but fast...
-
- return ( ( (mNode1 == inEdge.mNode1) && (mNode2 == inEdge.mNode2) ) ||
- ( (mNode1 == inEdge.mNode2) && (mNode2 == inEdge.mNode1) ) );
-
- }
-
- // ---------------------------------------------------------------------------
- // • HasMutualNode
- //
- // Called by: CEdge::Intersects
- // ---------------------------------------------------------------------------
- // Return true, if inEdge has at least one mutual node with this edge.
-
- inline Boolean
- CEdge::HasMutualNode( const CEdge & inEdge) const
- {
- return ( (mNode1 == inEdge.mNode1) ||
- (mNode2 == inEdge.mNode2) ||
- (mNode1 == inEdge.mNode2) ||
- (mNode2 == inEdge.mNode1) );
- }
-
- // ---------------------------------------------------------------------------
- // • OtherNode
- //
- // Called by: CGraphDrawing::ThreeConnectedNodes
- // ---------------------------------------------------------------------------
- // If inNode is one of this edge's two nodes, set outNode to point to this
- // edge's other node and return true. Otherwise leave outNode unchanged and
- // return false.
-
- inline Boolean
- CEdge::OtherNode( const CNodePtr & inNode, CNodePtr & outNode) const
- {
- Boolean theResult = false;
-
- if ( inNode == mNode1 ) {
- outNode = mNode2;
- theResult = true;
- }
- else
- if ( inNode == mNode2 ) {
- outNode = mNode1;
- theResult = true;
- }
-
- return theResult;
- }
-
- // ---------------------------------------------------------------------------
- // • GetEdgeNodes
- //
- // Called by: CGraphDrawing::EdgeMutation
- // CGraphDrawing::ThreeConnectedNodes
- // ---------------------------------------------------------------------------
- // Fetch this edge's nodepointers to the parameters outPtr1 & 2.
-
- inline void
- CEdge::GetEdgeNodes( CNodePtr &outPtr1, CNodePtr &outPtr2 ) const
- {
- outPtr1 = mNode1;
- outPtr2 = mNode2;
- }
-
- // ---------------------------------------------------------------------------
- // • GetNodeNumbers
- //
- // Called by: CGraphDrawing::CopyEdges
- // ---------------------------------------------------------------------------
- // Fetch the numbers of this edge's nodes to the parameters outNbr1 & 2.
-
- inline void
- CEdge::GetNodeNumbers( short & outNbr1, short & outNbr2) const
- {
- mNode1 -> GetNodeNbr( outNbr1 );
- mNode2 -> GetNodeNbr( outNbr2 );
- }
-
- // ---------------------------------------------------------------------------
- // • SetLength
- //
- // Called by: CGraphDrawing::CountEdgeLengths
- // ---------------------------------------------------------------------------
- // Update and return the _square_ of the length of the edge.
- // We don't want to use square roots as that would be TOO SLOW...
-
- inline Int16
- CEdge::SetLength( )
- {
- Int16 p1x, p2x, p1y, p2y;
-
- mNode1 -> GetNodePos( p1x, p1y );
- mNode2 -> GetNodePos( p2x, p2y );
-
- return ( mLength = ((p1x - p2x) * (p1x - p2x)) +
- ((p1y - p2y) * (p1y - p2y)) );
- }
-
- // ---------------------------------------------------------------------------
- // • HasMoved
- //
- // Called by: CGraphDrawing::SmarterCountSect
- // ---------------------------------------------------------------------------
- // Return whether the edge has moved or not
- // (it has, if either of the nodes has moved).
-
- inline Boolean
- CEdge::HasMoved() const
- {
- return ( mNode1 -> GetHasMoved() || mNode2 -> GetHasMoved() );
- }
-